<template>
    <a-modal v-model:visible="visible" :closable="false" :width="800" :wrap-style="{ overflow: 'hidden' }"
        @ok="onClose">

        <a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
            <a-row :gutter="24">

                <a-col :span="16">
                    <a-form-item name="name">
                        <a-input v-model:value="searchFormState.name" placeholder="请输入检索条件" />
                    </a-form-item>
                </a-col>

                <a-col :span="8" class="text-right">
                    <a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
                </a-col>
            </a-row>
        </a-form>
        <s-table ref="tableRef" rowKey="id" tableLayout="fixed" :row-selection="{
        type: 'radio',
        selectedRowKeys: selectedRowKeys, onChange: onSelectChange
    }" :size="5" :data="loadData" bordered :columns="columns" class="mb-2">
            <template #bodyCell="{ column, record, index }">
                <template v-if="column.key === 'index'">
                    {{ index + 1 }}
                </template>
            </template>
        </s-table>


    </a-modal>
</template>
<script setup>
//采购合同
import revenueContractApi from '@/api/contract/revenueContract/index'
//增补
import supplementApi from '@/api/contract/supplement/index'
//其他
import otherApi from '@/api/contract/other/index'
//收入合同
import purchaseApi from '@/api/contract/purchase/index'

import { ref } from 'vue';
import { number } from 'echarts';
const tableRef = ref()

const props = defineProps({
    type: {
        type: String,
        default: 0
    }
})
watch(
    () => props.type,
    (newVal, oldVal) => {
        if (newVal) {
            nextTick(() => {
                console.log("props.type",props.type)
                tableRef?.value?.refresh(true)
            })
        }
    },
    { deep: true, immediate: true }
)
const visible = ref(false)
const show = (contractId) => {
    visible.value = true
}

const searchFormState = ref({})


const loadData = (parameter) => {
    const apiMap = [
        revenueContractApi,
        purchaseApi,
        supplementApi,
        otherApi,
    ]
    searchFormState.value.current = parameter.current
    searchFormState.value.size = parameter.size
    const currentApi = apiMap[parseInt(props.type)];
    return currentApi.page(searchFormState.value).then((res) => {
        return res
    })
}

const selectedRowKeys = ref([])
const selectRecords = ref({})
const onSelectChange = (selectedKeys, selectedRows) => {
    selectedRowKeys.value = selectedKeys;
    selectRecords.value = selectedRows
    console.log('selectedRowKeys changed: ', selectedRowKeys);
}
const onselectRow = (columns, index) => {
    return {
        onClick: (event) => {
            console.log('onselectRow clicked', columns, index);
            // selectedRowKeys.value = [columns["id"]]
        },
    }
}
// const emits = defineEmits(['onOK'])
// const onOk = () => {
//     if (selectedRowKeys.value.length === 0) {
//         return
//     }
//     emits('onOK', selectedRowKeys)
// }



const emits = defineEmits(['onClose'])
const onClose = () => {
    visible.value = false;
    emits('onClose', selectedRowKeys.value, selectRecords.value)
};
defineExpose({
    show
});
const refresh = () => {


}
const columns = [
    {
        title: '合同编号',
        dataIndex: 'code',
        key: 'code',
        width: 160,
    },
    {
        title: '合同名称',
        dataIndex: 'name',
        key: 'name',
    },
    // {
    //     title: '甲方名称',
    //     dataIndex: 'partyA',
    //     key: 'partyA',
    // },
    {
        title: '合同日期',
        dataIndex: 'createTime',
        key: 'createTime',
    }, {
        title: '项目名称',
        dataIndex: 'proName',
        key: 'proName',
    },
]

</script>